package com.alks.yn_zs.service.serviceImpl;

import com.alibaba.excel.util.StringUtils;
import com.alks.yn_zs.data.dto.DeptProDto;
import com.alks.yn_zs.data.dto.StatsDto;
import com.alks.yn_zs.data.request.StatsRequest;
import com.alks.yn_zs.format.DateFormat;
import com.alks.yn_zs.mapper.ProdShowMapper;
import com.alks.yn_zs.service.IProdShowService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author MS
 * @since 2024/11/21上午9:06
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ProdShowServiceImpl implements IProdShowService {
    private final ProdShowMapper prodMapper;


    @Override
    public List<DeptProDto> blankList() {
        LocalDate startDate = LocalDate.now();
        return prodMapper.deptProList(startDate);
    }

    @Override
    public List<StatsDto> blankStats(StatsRequest request) {
        /*初始化*/
        String unit = StringUtils.isBlank(request.getTitleUnit()) ? DateFormat.DEF_UNIT : request.getTitleUnit();
        Integer pushUnit = request.getPushUnit() == null ? DateFormat.DEF_PUSH : request.getPushUnit();
        LocalDate endDate = request.getEndDate() == null ? DateFormat.DEF_END_DATE : request.getEndDate();
        LocalDate startDate = DateFormat.dateStartFormat(unit, pushUnit, endDate);
        String blankCode = "01";
        String matchCode = "03";
        String[] type = {"冲裁", "配套"};

        /*获取发料数据*/
        List<StatsDto> prodList = prodMapper.prodStatsQuery(startDate, endDate, blankCode);
        List<StatsDto> dtoList = new ArrayList<>();
        if (prodList==null){
            return dtoList;
        }
        Map<String, List<StatsDto>> prodGroupMap = prodList.stream()
                .collect(Collectors.groupingBy(a -> DateFormat.formatKey(unit, a.getDate())));
        prodGroupMap.forEach((key, value)->{
            StatsDto dto = new StatsDto();
            BigDecimal count = value.stream()
                    .map(StatsDto::getCount)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            dto.setDate(value.get(0).getDate());
            dto.setType("冲裁");
            dto.setTitle(key);
            dto.setCount(count);
            dtoList.add(dto);
            });
        /*获取配套数据*/
        List <StatsDto> matchList = prodMapper.prodStatsQuery(startDate, endDate, matchCode);
        if (matchList==null){
            return dtoList;
        }
        Map<String, List<StatsDto>> matchGroupMap = matchList.stream()
                .collect(Collectors.groupingBy(a -> DateFormat.formatKey(unit, a.getDate())));
        matchGroupMap.forEach((key, value)->{
            StatsDto dto = new StatsDto();
            BigDecimal count = value.stream()
                    .map(StatsDto::getCount)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            dto.setDate(value.get(0).getDate());
            dto.setType("配套");
            dto.setTitle(key);
            dto.setCount(count);
            dtoList.add(dto);
            });
        DateFormat.fillMissingAndSort(dtoList,unit,startDate,endDate,type);
        dtoList.sort(Comparator
                .comparing(StatsDto::getType)
                .thenComparing(StatsDto::getDate, Comparator.nullsFirst(Comparator.naturalOrder()))
        );
        return dtoList;
    }
}
